package com.baichuan.zntx.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.baichuan.common.idempotent.annotation.RepeatSubmit;
import com.baichuan.common.log.annotation.Log;
import com.baichuan.common.web.core.BaseController;
import com.baichuan.common.mybatis.core.page.PageQuery;
import com.baichuan.common.core.domain.R;
import com.baichuan.common.core.validate.AddGroup;
import com.baichuan.common.core.validate.EditGroup;
import com.baichuan.common.log.enums.BusinessType;
import com.baichuan.common.excel.utils.ExcelUtil;
import com.baichuan.zntx.domain.vo.MessageVo;
import com.baichuan.zntx.domain.bo.MessageBo;
import com.baichuan.zntx.service.IMessageService;
import com.baichuan.common.mybatis.core.page.TableDataInfo;

/**
 * 报警消息管理
 *
 * @author Baichuan
 * @date 2024-05-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zntx-app/message")
public class MessageController extends BaseController {

    private final IMessageService messageService;

    /**
     * 查询报警消息管理列表
     */
    @SaCheckPermission("zntx-app:message:list")
    @GetMapping("/list")
    public TableDataInfo<MessageVo> list(MessageBo bo, PageQuery pageQuery) {
        return messageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出报警消息管理列表
     */
    @SaCheckPermission("zntx-app:message:export")
    @Log(title = "报警消息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MessageBo bo, HttpServletResponse response) {
        List<MessageVo> list = messageService.queryList(bo);
        ExcelUtil.exportExcel(list, "报警消息管理", MessageVo.class, response);
    }

    @GetMapping("/read/{id}")
    public R read(@PathVariable("id") String id) {
        boolean res = messageService.readMessage(id);
        if(res){
            return R.ok();
        }
        return R.fail();
    }
    /**
     * 获取报警消息管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zntx-app:message:query")
    @GetMapping("/{id}")
    public R<MessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(messageService.queryById(id));
    }

    /**
     * 新增报警消息管理
     */
    @SaCheckPermission("zntx-app:message:add")
    @Log(title = "报警消息管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MessageBo bo) {
        return toAjax(messageService.insertByBo(bo));
    }

    /**
     * 修改报警消息管理
     */
    @SaCheckPermission("zntx-app:message:edit")
    @Log(title = "报警消息管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MessageBo bo) {
        return toAjax(messageService.updateByBo(bo));
    }

    /**
     * 删除报警消息管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zntx-app:message:remove")
    @Log(title = "报警消息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(messageService.deleteWithValidByIds(List.of(ids), true));
    }
}
